﻿#ifndef BCDEVICE_H
#define BCDEVICE_H

#include <QObject>
#include <QAudioDeviceInfo>
#include <QCameraInfo>

class QueueBuffer;
class LoopBuffer;
class QAudioInput;
class QAudioOutput;
class VideoSurface;
class GenerateImage;
class QThread;

class BCDevice : public QObject
{
    Q_OBJECT
public:
    static BCDevice *getInstance();

    Q_INVOKABLE QStringList getMicrophones();
    Q_INVOKABLE QStringList getLoudspeakers();
    Q_INVOKABLE QStringList getCameras();
public:
    Q_INVOKABLE int currentThreadId();
    GenerateImage * getGenerate();

signals:
    void sgStopMp3();
public slots:
    void dumpInfo();
    void devUpdate();

    Q_INVOKABLE void setAudioParam(int sampleRate,int channels,int sampleBites);

    Q_INVOKABLE bool startCamera(int index);
    Q_INVOKABLE bool startMicrophone(int index);
    Q_INVOKABLE bool startLoudspeaker(int index);

    Q_INVOKABLE bool stopCamera(int index);
    Q_INVOKABLE bool stopMicrophone(int index);
    Q_INVOKABLE bool stopLoudspeaker(int index);
    Q_INVOKABLE bool stopAll();

    Q_INVOKABLE bool playMp3(const QString &file);
    Q_INVOKABLE bool stopMp3();

private:
    static BCDevice * instance;
    explicit BCDevice(QObject *parent = nullptr);

    BCDevice(const BCDevice &sg) = delete;
    BCDevice &operator=(const BCDevice &sg) = delete;

    QList<QAudioDeviceInfo> s_micDevices;
    QList<QAudioDeviceInfo> s_loudSpeakerDevices;
    QList<QCameraInfo> s_cameras;

    int s_micIndex,s_loudspeakerIndex,s_cameraIndex;
    LoopBuffer *m_buffer = nullptr;

    int s_sampleRate,s_channels,s_sampleBits;

    QAudioFormat s_format;
    QAudioInput *s_audioInput = nullptr;
    QAudioOutput *s_audioOutput = nullptr;

    QCamera * m_camera = nullptr;
    VideoSurface *m_surface = nullptr;
    GenerateImage *m_generate = nullptr;

    QThread *m_mp3DecodeThread = nullptr;
    QThread *m_audioOutputThread = nullptr;
    LoopBuffer *m_mp3Buffer = nullptr;
    bool m_mp3IsPlaying = false;
};

#endif // BCDEVICE_H
